package com.ruoyi.web.controller.system;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SessionLoginUser;
//import com.ruoyi.common.utils.IpUtil;

import com.ruoyi.common.utils.JsonResult;
import com.ruoyi.common.utils.PageInfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.common.IpUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GuestbookAsk;
import com.ruoyi.system.service.IGuestbookAskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/system/ask")
public class GuestbookAskController extends BaseController
{
    @Autowired
    private IGuestbookAskService guestbookAskService;

    @Autowired
    private ISysUserService ISysUserService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:ask:list')")
    @GetMapping("/list")
    public TableDataInfo list(GuestbookAsk guestbookAsk)
    {
        startPage();
        List<GuestbookAsk> list = guestbookAskService.selectGuestbookAskList(guestbookAsk);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:ask:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GuestbookAsk guestbookAsk)
    {
        List<GuestbookAsk> list = guestbookAskService.selectGuestbookAskList(guestbookAsk);
        ExcelUtil<GuestbookAsk> util = new ExcelUtil<GuestbookAsk>(GuestbookAsk.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ask:query')")
    @GetMapping(value = "/{askid}")
    public AjaxResult getInfo(@PathVariable("askid") String askid)
    {
        return success(guestbookAskService.selectGuestbookAskByAskid(askid));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:ask:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GuestbookAsk guestbookAsk)
    {
        return toAjax(guestbookAskService.insertGuestbookAsk(guestbookAsk));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:ask:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GuestbookAsk guestbookAsk)
    {
        return toAjax(guestbookAskService.updateGuestbookAsk(guestbookAsk));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:ask:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{askids}")
    public AjaxResult remove(@PathVariable String[] askids)
    {
        return toAjax(guestbookAskService.deleteGuestbookAskByAskids(askids));
    }

    // 查询所有留言信息
    @RequestMapping("find_public")
    @ResponseBody
    public JsonResult<?> findPublic(
            @RequestParam("pageindex") int ppPageIndex,
            @RequestParam("pagesize") int ppPageSize,
            @RequestParam("ishuifu")int ppIsHuifu,
            @RequestParam("neirong")String ppNeirong) {
        try {
            int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
            int mmCount = guestbookAskService.countPublic(0,99999,ppIsHuifu,ppNeirong);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            List<Map<String, Object>> mmList = guestbookAskService.findPublic( mmStartIndex,
                    ppPageSize,ppIsHuifu,ppNeirong);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("PageInfo", mmPageInfo);
            mmMap.put("AskList", mmList);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    // 查询本科室（管理员）留言信息
    @RequestMapping("find_by_all")
    @ResponseBody
    public JsonResult<?> findByAll(@RequestParam("pageindex") int ppPageIndex,
                                   @RequestParam("pagesize") int ppPageSize,
                                   HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();

            SessionLoginUser mmLogin = (SessionLoginUser)session.getAttribute("sessionlogin");

            if (mmLogin==null){
                return JsonResult.getErrorResult("请先登录！");
            }

            String mmUserId = mmLogin.getLoginUserId();
            if (mmUserId==null){
                return JsonResult.getErrorResult("请先登录！");
            }

            int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
            int mmCount = guestbookAskService.count();
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            List<Map<String, Object>> mmList = guestbookAskService.findByAll(mmStartIndex, ppPageSize);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("PageInfo", mmPageInfo);
            mmMap.put("AskList", mmList);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    // 用户添加问题
    @RequestMapping("addcustom")
    @ResponseBody
    public JsonResult<?> addCustom(@RequestParam("shifouyinsi") String ppShifouyinsi,
                                   @RequestParam("title") String ppTitle,
                                   @RequestParam("content") String ppContent,
                                   @RequestParam("zhonglei") String ppZhonglei,
                                   HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();

            SessionLoginUser mmLogin = (SessionLoginUser)session.getAttribute("sessionlogin");

            if (mmLogin==null){
                return JsonResult.getErrorResult("请先登录！");
            }

            String mmUserId = mmLogin.getLoginUserId();
            if (mmUserId==null){
                return JsonResult.getErrorResult("请先登录！");
            }

            SysUser mmSysUser = ISysUserService.selectUserById(Long.parseLong(mmUserId));
            if (mmSysUser==null){
                return JsonResult.getErrorResult("用户不存在！");
            }

            GuestbookAsk mmGuestbookAsk = new GuestbookAsk();
            String mmAskId = UUID.randomUUID().toString();
            mmGuestbookAsk.setAskid(mmAskId);
            mmGuestbookAsk.setGuestbookid("院长政委信箱");
            mmGuestbookAsk.setUserid(mmUserId);
            mmGuestbookAsk.setNickname(mmLogin.getLoginName());
            mmGuestbookAsk.setShifouyinsi(Long.parseLong(ppShifouyinsi));
            mmGuestbookAsk.setLianxifangshi(mmSysUser.getPhonenumber());
            mmGuestbookAsk.setTitle(ppTitle);
            mmGuestbookAsk.setContent(ppContent);
            mmGuestbookAsk.setLiuyanshijian(new Date());
            mmGuestbookAsk.setIp(IpUtil.getIpAddr(request));
            mmGuestbookAsk.setZhonglei(ppZhonglei);

            if (guestbookAskService.insertGuestbookAsk(mmGuestbookAsk)==1) {
                return JsonResult.getSuccessResult(mmGuestbookAsk);
            } else {
                return JsonResult.getErrorResult("留言失败！");
            }

        } catch (Exception e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }
}
